package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.Post;
import com.zut.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

//    /*得到所有帖子*/
//    @GetMapping("/{pageNum}/{pageSize}")
//    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
//        PageInfo<Post> postPageInfo = postService.getAll(pageNum, pageSize);
//        Integer code = postPageInfo != null ? Code.GET_OK : Code.GET_ERR;
//        String msg = postPageInfo != null ? "" : "数据查询失败，请重试！";
//        return new Result(code, postPageInfo, msg);
//    }

    /*得到所有帖子*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> postPageInfo = postService.getAll(pageNum, pageSize);
        Integer code = postPageInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = postPageInfo != null ? "" : "数据查询失败，请重试！";
        return new Result(code, postPageInfo, msg);
    }


    /*根据科目得到帖子
     * 科目一 post/1/10/1
     * 科目二 post/1/10/2
     */
    @GetMapping("subject/{pageNum}/{pageSize}/{subject}")
    public Result getAllBySubject(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String subject) {
        PageInfo<Map<String, String>> postPageInfo = postService.getAllBySubject(pageNum, pageSize, subject);
        Integer code = postPageInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = postPageInfo != null ? "" : "数据查询失败，请重试！";
        return new Result(code, postPageInfo, msg);
    }

    /*根据时间排序得到帖子
     */
    @GetMapping("time/{pageNum}/{pageSize}")
    public Result getAllByTime(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> postPageInfo = postService.getAllByTime(pageNum, pageSize);
        Integer code = postPageInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = postPageInfo != null ? "" : "数据查询失败，请重试！";
        return new Result(code, postPageInfo, msg);
    }

    /*根据点击次数排序排序得到帖子
     * 降序为 post/click/1/10/
     */
    @GetMapping("click/{pageNum}/{pageSize}")
    public Result getAllByClick(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> postPageInfo = postService.getAllByClick(pageNum, pageSize);
        Integer code = postPageInfo != null ? Code.GET_OK : Code.GET_ERR;
        String msg = postPageInfo != null ? "" : "数据查询失败，请重试！";
        return new Result(code, postPageInfo, msg);
    }


    /*发布帖子*/
    @PostMapping
    public Result insert(@RequestBody Post post) {
        boolean flag = postService.insert(post);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据id删除帖子*/
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        boolean flag = postService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*更新帖子信息
     * 更新问题是否解决
     */
    @PutMapping
    public Result Update(@RequestBody Post post) {
        boolean flag = postService.update(post);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据帖子id 点击次数+1*/
    @PostMapping("click/{id}")
    public Result updateClick(@PathVariable String id) {
        boolean flag = postService.updateClick(id);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据帖子id获取详细信息*/
    @GetMapping("{id}")
    public Result getById(@PathVariable String id) {
        Map<String, String> map = postService.getById(id);
        Integer code = map != null ? Code.GET_OK : Code.GET_ERR;
        String msg = map != null ? "" : "数据查询失败，请重试！";
        return new Result(code, map, msg);
    }

    /*查询发帖次数，回帖次数，点赞次数
    json样例：
     {
        "id":"001",
        "type":"学员"
     }
     */
    @PostMapping("/message")
    public Result test(@RequestBody HashMap<String, String> map) {
        String id = map.get("id");
        String type = map.get("type");
        Map<String, String> message = postService.getMessage(id, type);
        Integer code = message != null ? Code.GET_OK : Code.GET_ERR;
        String msg = message != null ? "" : "数据查询失败，请重试！";
        return new Result(code, message, msg);
    }

    /*根据帖子id获取详细信息*/
    @GetMapping("/user/{id}")
    public Result getPostByUserId(@PathVariable String id) {
        List<Map<String, String>> map = postService.getPostByUserId(id);
        Integer code = map != null ? Code.GET_OK : Code.GET_ERR;
        String msg = map != null ? "" : "数据查询失败，请重试！";
        return new Result(code, map, msg);
    }
}
